package xmlGen;

import org.jdom.Attribute;
import org.jdom.Element;

import executer.Parser;

import modele.Anniv;
import modele.Contact;
import modele.Event;
import modele.Owner;
import modele.User;

public class JDomElementCreater {

	private Element root;
	
	public JDomElementCreater(Element root) {
		
		this.root = root;
	}
	
	
	public void addElement(Contact contact) {
		
		this.root.addContent(createElement(contact));
	}
	
	
	public void addElement(Event event) {
		
		this.root.addContent(createElement(event));
	}
	
	
	public void addElement(Owner owner) {

		/*Element elt = new Element("Nom");
		elt.addContent(owner.getNom());
		root.addContent(elt);
		elt = new Element("Prenom");
		elt.addContent(owner.getPrenom());
		root.addContent(elt);
		elt = new Element("Login");
		elt.addContent(owner.getLogin());
		root.addContent(elt);
		elt = new Element("BirthDay");
		elt.addContent(owner.getDateToStr());
		root.addContent(elt);
		elt = new Element("Id");
		elt.addContent(Integer.toString(owner.getId()));
		root.addContent(elt);*/
		Element parent = createElement(owner);
		Element elt = new Element("Login");
		elt.addContent(owner.getLogin());
		parent.addContent(elt);
		parent.setName("Owner");
		root.addContent(parent);
	}
	
	
	public static Element createElement(User user) {

		Element parent = new Element("User");
		Element elt = new Element("Nom");
		elt.addContent(user.getNom());
		parent.addContent(elt);
		elt = new Element("Prenom");
		elt.addContent(user.getPrenom());
		parent.addContent(elt);
		elt = new Element("BirthDay");
		elt.addContent(Parser.parseToString(user.getDateNaissance()));
		parent.addContent(elt);
		parent.setAttribute("idUser", Integer.toString(user.getId()));
		
		return parent;
	}


	public static Element createElement(Event event) {
		
		Element parent = new Element("Event");
		parent.setAttribute("idEvent", Integer.toString(event.getId()));
		parent.setAttribute("type", event.getDescription());
		if(event.getDescription().equals("Anniversaire")) {
			parent.addContent(createElement(((Anniv)event).getUtilisateur()));
		}
		Element child = new Element("Commentaire");
		child.addContent(event.getCommentaire());
		parent.addContent(child);
		child = new Element("Date");
		child.addContent(Parser.parseToString(event.getDate().getTime()));
		parent.addContent(child);
		
		return parent;
	}
	
	
	public static Element createElement(Contact contact) {
		
		Element parent = new Element("Contact");
		Attribute attribute = new Attribute("id", Integer.toString(contact.getId()));
		parent.setAttribute(attribute);
		Element child = new Element("Nom");
		child.addContent(contact.getNom());
		parent.addContent(child);
		child = new Element("Prenom");
		child.addContent(contact.getPrenom());
		parent.addContent(child);
		child = new Element("Adresse");
		child.addContent(contact.getAdresse());
		parent.addContent(child);
		child = new Element("Ville");
		child.addContent(contact.getVille());
		parent.addContent(child);
		child = new Element("Pays");
		child.addContent(contact.getPays());
		parent.addContent(child);
		child = new Element("Telephone");
		Element elt = new Element("Fixe");
		elt.addContent(contact.getFixe());
		child.addContent(elt);
		elt = new Element("Portable");
		elt.addContent(contact.getPortable());
		child.addContent(elt);
		parent.addContent(child);
		child = new Element("Email");
		child.addContent(contact.getEmail());
		parent.addContent(child);
		
		return parent;
	}
}
